package com.tz.actions;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.tz.entity.Cart;
import com.tz.entity.OrderItem;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Created by heziqian on 2016/10/21.
 */
@Namespace("/")
public class PageAction extends ActionSupport{

    @Action(value = "myaccount",
            results = {@Result(location="/myaccount.jsp"),
                    @Result(name = "input",location = "/myaccount.jsp")
            })
    public String execute() {

        return SUCCESS;

    }


    @Action(value = "toregister",
            results = {@Result(location="/register.jsp"),
                    @Result(name = "input",location = "/myaccount.jsp")
            })
    public String execute5() {

        return SUCCESS;

    }





    @Action(value = "index",
            results = {@Result(location="/index.jsp"),
                    @Result(name = "input",location = "/index.jsp")
            })
    public String execute2() {

        ActionContext context=ActionContext.getContext();
        Map<String,Object> session=context.getSession();
        Cart cart=(Cart)session.get("cart");
        if(cart==null){
            Cart c=new Cart();
            Set<OrderItem> items=new HashSet<>();
            c.setCount(0);
            c.setTotalPrice(0);
            c.setItems(items);
            session.put("cart",c);
        }
        return SUCCESS;

    }



}
